﻿$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        dataType: "json",
        url: "../../Ajax/ChanceTypeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                var id = 0;
                var chanceTypeName = "";
                $.each(json, function (k, v) {
                    if (k == 0) { id = v.ID; chanceTypeName = v.Name; }
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblID' id='" + v.ID + "'>";
                    tbody += v.ID;
                    tbody += "</label></td>";
                    tbody += "<td><a href='javascript:void(0)' onclick='javascript:ChangeChanceType(this)'><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.Name;
                    tbody += "</label></a></td>";
                    tbody += "<td><lable name='lblHandCount'>";
                    tbody += v.HandCount == "" ? "0" : v.HandCount;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='UpdateType(this)' value='更新'><input type='button' onclick='DeleteType(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
                LoadChance(id, chanceTypeName);
                LoadEvent(id, chanceTypeName);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });


    $("#btnAdd").click(function () {
        EditType(null);
    })
})

function LoadChance(id, chanceTypeName) {
    if (id > 0) {
        $("#lblChanceTypeName").text(chanceTypeName);
        $.ajax({
            type: "post",
            data: { action: "GetList", typeID: id },
            dataType: "json",
            url: "../../Ajax/ChanceAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data != null) {
                    var json = data.json;
                    var tbody = "";
                    $.each(json, function (k, v) {
                        tbody += "<tr>"
                        tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                        tbody += v.ChanceName;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblValue'>";
                        tbody += v.ChanceValue == "" ? "0" : v.ChanceValue;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblMax'>";
                        tbody += v.MaxNum == "" ? "0" : v.MaxNum;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblMin'>";
                        tbody += v.MinNum == "" ? "0" : v.MinNum;
                        tbody += "</label></td>";
                        tbody += "<td>";
                        tbody += "<input type='button' onclick='UpdateChance(this)' value='更新'>"
                        tbody += "</td>";
                        tbody += "</tr>"
                    })
                    $("#tb2").empty().append(tbody);

                }
            },
            complete: function () {
            },
            error: function () {
            }
        });
    }
}

function LoadEvent(id, chanceTypeName) {
    if (id > 0) {
        $("#lblChanceTypeName2").text(chanceTypeName);
        $.ajax({
            type: "post",
            data: { action: "GetList" },
            dataType: "json",
            url: "../../Ajax/EventAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data != null) {
                    var json = data.json;
                    var tbody = "";
                    var eventID = 0;
                    var eventName = "";
                    $.each(json, function (k, v) {
                        if (k == 0) { eventID = v.ID; eventName = v.Name; }
                        tbody += "<tr>";
                        tbody += "<td><a href='javascript:void(0)' onclick='javascript:LoadEventChance(" + id + "," + v.ID + ",\"" + v.Name + "\")'><lable name='lblName' id='" + v.ID + "'>";
                        tbody += v.Name;
                        tbody += "</label></a></td>";
                        tbody += "</tr>"
                    })
                    $("#tb3").empty().append(tbody);
                    LoadEventChance(id, eventID, eventName);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        });
    }
}

function ChangeEventChance(contr) {
    var lblValue = $(contr).parent().parent().find("lable[name=lblValue]").first();
    var id = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    var content = "</br><table><tr><td>概率值：<input type='text' id='txtValue' value='" + $(lblValue).text() + "' style='width:200px' /></td></tr></table></br>";
    $.dialog({
        title: "更新事件概率值",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var chanceValue = $("#txtValue").val();
                            if ($.trim(chanceValue) == "") {
                                $.dialog.alert("请输入概率值！");
                                return false;
                            }
                            $.ajax({
                                type: "post",
                                data: { action: "Update", id: id, chanceValue: chanceValue },
                                async: true,
                                url: "../../Ajax/EventChanceAjax1.ashx",
                                beforeSend: function () {
                                },
                                success: function (data) {
                                    if (data == "True") {
                                        $(lblValue).text(chanceValue);
                                        $.dialog.tips("更新成功！", 1);
                                    }
                                    else {
                                        $.dialog.tips("更新失败！", 1);
                                    }

                                },
                                complete: function () {
                                },
                                error: function () {
                                }
                            })
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false

    });
}

function LoadEventChance(id, eventID, eventName) {
    if (id > 0 && eventID > 0) {
        $("#lblEventName").text(eventName);
        $.ajax({
            type: "post",
            data: { action: "GetList", typeID: id, eventID: eventID },
            dataType: "json",
            url: "../../Ajax/EventChanceAjax1.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data != null) {
                    var json = data.json;
                    var tbody = "";
                    var eventID = 0;
                    var typeName = "";
                    $.each(json, function (k, v) {
                        if (k == 0) { id = v.ID; typeName = v.ChanceName; }
                        tbody += "<tr>";
                        tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                        tbody += v.MouldName;
                        tbody += "</label></td>";
                        tbody += "<td><lable name='lblValue'>";
                        tbody += v.ChanceValue;
                        tbody += "</label></td>";
                        tbody += "<td>";
                        tbody += "<input type='button' onclick='ChangeEventChance(this)' value='更新'>"
                        tbody += "</td>";
                        tbody += "</tr>"
                    })
                    $("#tb4").empty().append(tbody);

                }
            },
            complete: function () {
            },
            error: function () {
            }
        });
    }
}

function ChangeChanceType(contr) {
    var parent = $(contr).parent().parent();
    var id = $(parent).find("lable[name=lblID]").first().text();
    var typeName = $(parent).find("lable[name=lblName]").first().text();
    //alert(typeName)
    LoadChance(id, typeName);
    LoadEvent(id, typeName);
}

function UpdateChance(contr) {
    var id = $(contr).parent().parent().find("lable[name=lblName]").attr("id"); //概率id
    var contrValue = $(contr).parent().parent().find("lable[name=lblValue]"); //概率值
    var maxValue = $(contr).parent().parent().find("lable[name=lblMax]"); //概率值
    var minValue = $(contr).parent().parent().find("lable[name=lblMin]"); //概率值
    var content = "<table><tr><td>概率值：<input type='text' id='txtValue' value='" + $(contrValue).text() + "' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>最大命中次数：<input type='text' id='txtMax' value='" + $(maxValue).text() + "' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>最小命中次数：<input type='text' id='txtMin' value='" + $(minValue).text() + "' style='width:200px;float:right' /></td></tr></table>";
    $.dialog({
        title: "更新概率值",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var chanceValue = $("#txtValue").val();
                            var maxNum = $("#txtMax").val();
                            var minNum = $("#txtMin").val();
                            if ($.trim(chanceValue) == "") {
                                $.dialog.alert("请输入概率值！");
                                return false;
                            }
                            $.ajax({
                                type: "post",
                                data: { action: "Update", id: id, chanceValue: chanceValue, maxNum: maxNum, minNum: minNum },
                                async: true,
                                url: "../../Ajax/ChanceAjax.ashx",
                                beforeSend: function () {
                                },
                                success: function (data) {
                                    if (data == "True") {
                                        $(contrValue).text(chanceValue);
                                        $(maxValue).text(maxNum);
                                        $(minValue).text(minNum);
                                        $.dialog.tips("更新成功！", 1);
                                    }
                                    else {
                                        $.dialog.tips("更新失败！", 1);
                                    }
                                },
                                complete: function () {
                                },
                                error: function () {
                                }
                            })
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function EditType(contr) {
    //lbl 是判断是添加还是修改  添加为null
    var content = "";
    var lblHandCount = $(contr).parent().parent().parent().find("lable[name=lblHandCount]").first();
    var a = $(lblHandCount).attr("name");
    if (contr != null)
        content = "<table><tr><td>概率名称：<input type='text' id='txtName' value='" + $(contr).text() + "' style='width:200px;float:right' /></td></tr>"
                + "<tr><td>手数：<input type='text' id='txtHandCount' value='" + $(lblHandCount).text() + "' style='width:200px;float:right' /></td></tr></table>";
    else
        content = "<table><tr><td>概率号：<input type='text' id='txtID' style='width:200px;float:right' /></td></tr>"
                        + "<tr><td>概率名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>"
                        + "<tr><td>手数：<input type='text' id='txtHandCount' value='0' style='width:200px;float:right' /></td></tr></table>";
    $.dialog({
        title: "更新概率",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            //                            var type = "^[0-9]*[1-9][0-9]*$";
                            //                            var re = new RegExp(type);

                            var objectName = $("#txtName").val();
                            var id = $("#txtID").val();
                            var handCount = $("#txtHandCount").val();
                            if ($.trim(objectName) == "") {
                                $.dialog.alert("请输入概率号！");
                                return false;
                            }
                            //                            if (id.match(re) == null) {
                            //                                $.dialog.alert("请输入正整数!");
                            //                                return false;
                            //                            }
                            if ($.trim(objectName) == "") {
                                $.dialog.alert("请输入概率名称！");
                                return false;
                            }
                            var data = contr == null ? { action: "Add", name: objectName, id: id, handCount: handCount} :
                                                       { action: "Update", name: objectName, id: $(contr).attr("id"), handCount: handCount };
                            $.ajax({
                                type: "post",
                                data: data,
                                async: true,
                                url: "../../Ajax/ChanceTypeAjax.ashx",
                                beforeSend: function () {
                                },
                                success: function (data) {
                                    if (data == "True") {
                                        $.dialog.tips("更新成功！", 1);
                                        if (contr == null) {
                                            var tbody = "<tr>"
                                            tbody += "<td><lable name='lblID' id='" + id + "'>";
                                            tbody += id
                                            tbody += "</label></td>";
                                            tbody += "<td><a href='javascript:void(0)' onclick='javascript:ChangeChanceType(this)'><lable name='lblName' id='" + id + "'>";
                                            tbody += objectName;
                                            tbody += "</a></label></td>";
                                            tbody += "<td><lable name='lblHandCount'>";
                                            tbody += handCount;
                                            tbody += "</label></td>";
                                            tbody += "<td>";
                                            tbody += "<input type='button' onclick='UpdateType(this)' value='更新'><input type='button' onclick='DeleteType(this)' value='删除'>"
                                            tbody += "</td>";
                                            tbody += "</tr>"
                                            $("#tb1").append(tbody);
                                        }
                                        else {
                                            $(contr).text(objectName);
                                            $(lblHandCount).text(handCount);
                                        }
                                    }
                                    else {
                                        $.dialog.tips("更新失败！", 1);
                                    }
                                },
                                complete: function () {
                                },
                                error: function () {
                                }
                            })
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function UpdateType(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    EditType(lbl);
}

function DeleteType(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblID]").first();
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: $(lbl).attr("id") },
            async: true,
            url: "../../Ajax/ChanceTypeAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(lbl).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
    